const fs = require("fs")
const sysPath = require("path")
const packages = fs.readdirSync(sysPath.resolve(__dirname, "../../packages"))
  .filter(template => template.indexOf("plugin") > -1)
//移出老版本的目录
let vuePackages = packages.filter((template) => (template.indexOf("vue") > -1 && template.indexOf("old") == -1))
let reactPackages = packages.filter((template) => (template.indexOf("vue") == -1 && template.indexOf("svelte") == -1 && template.indexOf("old") == -1))

resetPackagesConfig([...vuePackages, ...reactPackages])
resetExampleConfig()

function resetExampleConfig() {
  fs.readdir(sysPath.resolve(__dirname, "../../example"), (err, example) => {
    if (err) {
      console.log(err);
      return
    }
    example.forEach((item1) => {
      fs.readdirSync(sysPath.resolve(__dirname, `../../example/${item1}`)).forEach((item2) => {
        fs.readdirSync(sysPath.resolve(__dirname, `../../example/${item1}/${item2}`)).forEach((item3) => {
          try {
            resetHandle(`../../example/${item1}/${item2}/${item3}`)
          } catch (e) {

          }
        })
      })
    })
  })

}

function resetPackagesConfig(packages) {
  packages.forEach(template => {
    try {
      resetHandle(`../../packages/${template}`)
    } catch (e) {
    }
    /*if (fs.existsSync(path.resolve(__dirname, `../../packages/${template}/output`))) {
     let outputFiles = fs.readdirSync(path.resolve(__dirname, `../../packages/${template}/output`))
     outputFiles.forEach((item) => {
     if (item.endsWith(".zip")) {
     fse.removeSync(path.resolve(__dirname, `../../packages/${template}/output/${item}`))
     }
     })
     }*/

  })
}

function resetHandle(basePath) {
  let configJson = require(`${basePath}/pluginTemp/config.json`)
  configJson["online-development-mode"] && (configJson["online-development-mode"].open = false)
  delete configJson["requirement-number"]
  delete configJson["requirement-name"]
  fs.writeFileSync(
    sysPath.resolve(__dirname, `${basePath}/pluginTemp/config.json`),
    JSON.stringify(configJson, null, 2),
    "utf8"
  )
  let informationJson = require(`${basePath}/scripts/information.json`)
  informationJson["developers"] = ""
  informationJson["ipAdress"] = ""
  delete informationJson.baseInformationSet
  fs.writeFileSync(
    sysPath.resolve(__dirname, `${basePath}/scripts/information.json`),
    JSON.stringify(informationJson, null, 2),
    "utf8"
  )
  fs.writeFileSync(
    sysPath.resolve(__dirname, `${basePath}/.env`),
    "",
    "utf8"
  )

}

